﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BooksManagermentSystem
{
    public partial class DeleteBook_form : Form
    {
        public DeleteBook_form()
        {
            InitializeComponent();
        }

        private void DeleteBook_form_Load(object sender, EventArgs e)
        {
            Selectbookid();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string bookid = comboBox1.Text.ToString();

            string sql = "select * from BookInfo,BookType,Press,Pressmark " +
                "where BookInfo.typeid = BookType.typeid " +
                "  and BookInfo.pressid =Press.pressid " +
                "  and BookInfo.pressmarkid = Pressmark.pressmarkid " +
                $"  and bookid = '{bookid}'";

            try
            {
                DataRow dr = DBhelper.GetDataRow(sql);
                textBox1.Text = dr["bookname"].ToString();
                textBox2.Text = dr["typename"].ToString();
                textBox3.Text = dr["bookauthor"].ToString();
                textBox4.Text = dr["pressname"].ToString();
                textBox5.Text = dr["pressmarknumber"].ToString();
            }
            catch(Exception)
            {

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string bookid = comboBox1.Text;
            DialogResult rs = MessageBox.Show("确定要下架吗？","提示",MessageBoxButtons.YesNo);
            if(rs == DialogResult.Yes)
            {
                string sql = $"delete from BookInfo where bookid='{bookid}'";
                if(DBhelper.NonQuery(sql))
                {
                    MessageBox.Show("下架成功");
                    Selectbookid();
                }
                else
                {
                    MessageBox.Show("下架失败");
                }
            }
        }

        private void Selectbookid()
        {
            string sql = "select * from BookInfo";
            DataTable dt = DBhelper.GetDataTable(sql);
            comboBox1.DataSource = dt;
            comboBox1.DisplayMember = "bookid";
        }
    }
}
